/**
 * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.addon.interact.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import com.jfinal.plugin.activerecord.Record;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.addon.interact.model.JpressAddonInteract;
import io.jpress.addon.interact.model.JpressAddonInteractComment;
import io.jpress.addon.interact.service.JpressAddonInteractCommentService;
import io.jpress.addon.interact.service.JpressAddonInteractService;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.web.base.AdminControllerBase;


@RequestMapping(value = "/admin/interact/jpress_addon_interact_comment", viewPath = "/")
public class _JpressAddonInteractCommentController extends AdminControllerBase {

    @Inject
    private JpressAddonInteractCommentService service;
    @Inject
    private JpressAddonInteractService iservice;

    @AdminMenu(text = "信息解答", groupId = "interact")
    public void index() {

        //问题
        String title = getPara("title");
        //提问人
        String inickname = getPara("inickname");
        //解答人
        String nickname = getPara("nickname");

        Record ifirst = null;
        if(inickname!=null)
            ifirst = Db.findFirst("SELECT a.* FROM  ADDON_INTERACT a,USER_ADMIN u WHERE a.USER_ID = u.id AND u.nickname = ?", inickname);
        Record first = null;
        if(nickname!=null)
            first = Db.findFirst("SELECT a.* FROM  ADDON_INTERACT_COMMENT a,USER_ADMIN u WHERE a.USER_ID = u.id AND u.nickname = ?", nickname);

        JpressAddonInteract firstByColumns = null;
        if(title!=null) {
            Columns icolumns = Columns.create();
            icolumns.likeAppendPercent("title", title);
            firstByColumns = iservice.findFirstByColumns(icolumns);
        }

        Columns columns = Columns.create();
        if(ifirst!= null) columns.eq("interact_id", ifirst.get("id"));
        if(first!=null) columns.eq("user_id", first.get("id"));
        if(firstByColumns!=null) columns.eq("interact_id", firstByColumns.get("id"));

        Page<JpressAddonInteractComment> entries=service.paginateByColumns(getPagePara(), 10,columns);
        setAttr("page", entries);
        render("views/jpress_addon_interact_comment_list.html");
    }


    public void edit() {
        int entryId = getParaToInt(0, 0);
        JpressAddonInteractComment entry = entryId > 0 ? service.findById(entryId) : null;
        setAttr("jpressAddonInteractComment", entry);
        render("views/jpress_addon_interact_comment_edit.html");
    }

    public void doEdit() {
        JpressAddonInteractComment entry = getModel(JpressAddonInteractComment.class,"jpressAddonInteractComment");
        JpressAddonInteractComment jpressAddonInteractComment = service.findById(entry.getId());
        jpressAddonInteractComment.setContent(entry.getContent());
        service.saveOrUpdate(jpressAddonInteractComment);
        renderJson(Ret.ok().set("id", entry.getId()));
    }

    public void doSave() {
        JpressAddonInteractComment entry = getModel(JpressAddonInteractComment.class,"jpressAddonInteractComment");
        service.saveOrUpdate(entry);
        renderJson(Ret.ok().set("id", entry.getId()));
    }

    public void doDel() {
        Long id = getIdPara();
        render(service.deleteById(id) ? Ret.ok() : Ret.fail());
    }

    @EmptyValidate(@Form(name = "ids"))
    public void doDelByIds() {
        service.batchDeleteByIds(getParaSet("ids").toArray());
        renderOkJson();
    }
}
